﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading;
using System.Xml;
using SDT.ServiceSystem;
using SDT.DataService.Services;
using SDT.DataService;

namespace SDT.CrawlSystem.MediaSrv
{
    /// <summary>
    /// Written by TungNX
    /// </summary>
    public sealed partial class MDCrawler : WorkerThread
    {
        private MDContainer TheContainer = null;
        
        #region Properties from data file

        private int interval = Global.Instance.Interval;
        public override int Interval
        {
            get { return interval; }
        }

        private int workUnitInterval =Global.Instance.WorkUnitInterval;
        public override int WorkUnitInterval
        {
            get { return workUnitInterval; }
        }
        public int CurThread { get; set; }

       
        #endregion

        #region Constructors
        public MDCrawler(int curThread, IDictionary<int, IList<MDWorkUnit>> workUnitList,  MDContainer container)
            : base()
        {
            TheContainer = container;
            WorkUnitList = workUnitList;
            CurThread = curThread;
           
        }
        #endregion

        private int CurrentWorkIndex = 0;
        private IDictionary<int, IList<MDWorkUnit>> WorkUnitList = new Dictionary<int, IList<MDWorkUnit>>();
        private Spider_SiteService svSite = new Spider_SiteService();
        private CategoryService svCate = new CategoryService();

        protected override bool CanStop
        {
            get { return Interlocked.Equals(CurrentWorkIndex, WorkSize); }
        }

        public override void DoWork()
        {
            while (true)
            {
                LoadWork();
                // Get work item
                var w = GetWork();
                if (w != null && w.Ready)
                {
                    lock (w)
                    {   
                        CurrentWorkIndex++;
                      //  w.Ready = false;
                     
                        MDWorkUnit itm = (MDWorkUnit)w;
                        lock (itm)
                        {
                            itm.LoadUnitWork(itm.LinkID, itm.CreateDate, itm.Url, itm.CID, itm.PID);
                        }
                    }
                    Thread.Sleep(WorkUnitInterval);
                }

                Thread.Sleep(WorkUnitInterval);
              
            }

        }

        private void LoadWork()
        {
            if (CurrentWorkIndex == WorkSize && CurrentWorkIndex == 0)
            {
                foreach (var l in WorkUnitList.Values)
                    foreach (var work in l)
                        AddWork(work);
            }
            else if (WorkSize == 0 && CurrentWorkIndex !=0)
            {
                lock (TheContainer)
                {
                    TheContainer.Remove(this);
                }
            }
        }

        private static bool SaveResult(IWorkUnit w)
        {  
            return true;
        }
       
    }
}